package la.bbs558.aop.delay.handler;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import la.bbs558.aop.delay.DelayQueueManager;
import la.bbs558.aop.delay.DelayTask;
import la.bbs558.entity.BbsClass;
import la.bbs558.entity.BbsClassActive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @Classname DelayUserHandler
 * @Description TODO
 * @Date 2023/11/11 17:37
 * @Created by weihanmin
 */
@Component
@Slf4j
public class BbsClassActiveEndDelayHandler extends BbsClassActiveDelayHandler {
    @Override
    public void doHandle(DelayTask<?> delayTask, DelayQueueManager delayQueueManager) {
        Serializable entity = delayTask.getEntity();
        if(entity instanceof BbsClass){
            BbsClassActiveContext activeContext = initCtx(((BbsClass) entity).getId());
            BbsClass bbsClass = activeContext.getBbsClass();
            BbsClassActive bbsClassActive = activeContext.getBbsClassActive();
            //设置主题活动为已结束
            DateTime now = DateUtil.date();
            if(now.isBefore(bbsClassActive.getEndTime())){
                //扔回延时队列
                delayQueueManager.addTask(delayTask);
                return;
            }
            bbsClass.setStatus(0);
            bbsClass.setClassName("已结束");
            bbsClass.updateById();
            bbsClassActive.setStatus(0);
            bbsClassActive.updateById();
        }
    }
    @Override
    public void doHandleError(DelayTask<?> delayTask, DelayQueueManager delayQueueManager, Throwable e) {
        log.error("主题活动处理出现异常");
    }

}
